package com.example.onlinemusic.controller;

import com.example.onlinemusic.mapper.LoveMusicMapper;
import com.example.onlinemusic.mapper.MusicMapper;
import com.example.onlinemusic.model.LoveMusic;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.User;
import com.example.onlinemusic.tools.Constant;
import com.example.onlinemusic.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Autowired
    LoveMusicMapper loveMusicMapper;
    @Resource
    MusicMapper musicMapper;

    /**
     * 收藏音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request) {

        int musicId = Integer.parseInt(id);
        System.out.println("loveMusicId->" + musicId);

        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            return new ResponseBodyMessage<>(-1,"未登录",false);
        }

        User user = (User)session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId->" + userId);
        //查询喜欢的音乐是否已经收藏
        Music music = loveMusicMapper.findLoveMusic(userId,musicId);
        if(music == null) {
            //说明该音乐未收藏
            int ret = loveMusicMapper.insertLoveMusic(userId,musicId);
            if(ret == 1) {
                return new ResponseBodyMessage<>(1,"收藏成功",true);
            }else {
                return new ResponseBodyMessage<>(-2,"收藏失败",false);
            }
        }else {
            //说明该音乐已收藏
            int ret = loveMusicMapper.deleteLoveMusic(userId,musicId);
            if(ret == 1) {
                return new ResponseBodyMessage<>(1,"取消收藏成功",true);
            }else {
                return new ResponseBodyMessage<>(-3,"取消收藏失败",false);
            }
        }
    }

    /**
     * 查找自己收藏的音乐
     * @param musicName
     * @param request
     * @return
     */
    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findLoveMusic(@RequestParam(required = false)String musicName,HttpServletRequest request) {

        HttpSession session = request.getSession(false);
        if(session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            return new ResponseBodyMessage<>(-1,"未登录",null);
        }

        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("用户id->" + userId);

        List<Music> list = new ArrayList<>();
        List<LoveMusic> loveMusics;

        System.out.println("----------------" + list.size() + "------------------");


        Music music = null;
        int index = 0;

        loveMusics = loveMusicMapper.findLoveMusicByUID(userId);
        System.out.println("----------------" + loveMusics.size() + "------------------");

        if (loveMusics.size() ==  0) {
            return new ResponseBodyMessage<>(0,"没有的收藏的音乐┭┮﹏┭┮",null);
        }


        if(musicName == null) {
            System.out.println("----------------无参方法，找到所有收藏的音乐------------------");

            for (int i = 0; i < loveMusics.size(); i++) {
                index = loveMusics.get(i).getMusicid();
                music = musicMapper.findMusicById(index);
                list.add(music);
            }

        }else {
            System.out.println("----------------有参方法，找到关键词音乐------------------");

            for (int i = 0; i < loveMusics.size(); i++) {
                index = loveMusics.get(i).getMusicid();
                music = musicMapper.findMusicByNameAndMID(musicName,index);
                if(music == null) {
                    continue;
                }else {
                    list.add(music);
                }
            }
        }

        System.out.println("----------------" + list.size() + "------------------");


        if(list.size() == 0) {
            return new ResponseBodyMessage<>(0,"没有收藏该音乐",null);
        }
        return new ResponseBodyMessage<>(0,"查询到收藏的音乐",list);
    }
}
